import publicForm from "@/components/public/up-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    getJobbriefingdataByfId, //详情
    saveJobbriefingdata, //保存修改
    getWeeks, //根据总结日期得到周次

} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "projectId",
        "status",
        "statusNum",
        "dataFid",
        "currentRow",
        "projectName",
        "fCompanyid"
    ],
    data() {
        return {
            visible: "",
            title: "本周工作简报",
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            formData: {
                fId: "", //选中简报的ID
                fProjectid: "", //项目ID
                fProjectname: "", //项目名称
                fCompanyid: "", //公司ID
                fCompanyname: "", //公司名
                fSubunitid: "", //周简报提交单位ID
                fSubunit: "", //周简报提交单位名
                fReportid: "", //填报人ID
                fReportname: "", //填报人名
                fWeeklyreportdates: "", //周简报日期
                fReportingweek: "", //填报周次
                fBriefingoverview: "", //工作简报概述
                fReportdates: "", //填报日期
                fAnnex: "", //资料附件ID
                recordfiles: [], //删除文件id集合
                userId: "", //登录用户id
                userName: "" //登录用户名
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            startDates: null,
            currentStatus: null,
            isLock: false
        };
    },
    components: {
        publicForm,
        // commonPage
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        projectId: {
            handler(newVal) {
                if (newVal) {
                    // console.log(newVal);
                    // this.getPerson(newVal);
                }
            },
            immediate: true,
        },
        projectName: {
            handler(newVal) {
                if (newVal) {
                    // console.log(newVal);
                    // this.getPerson(newVal);
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    this.btnStatus = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        dataFid: {
            handler(newVal) {
                if (newVal) {
                    // console.log(newVal);
                    this.currntData = newVal;
                    if (this.statusNum != 1) {
                        this.formData = this.$options.data.call(this).formData;
                        this.getDetail({
                            fId: newVal,
                            fCompanyid: this.fCompanyid
                        });
                    }
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    // this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        this.btnShow = true
        if (this.statusNum == 1) {
            this.formData = this.$options.data.call(this).formData;
            this.getDetail({
                fId: "",
                fCompanyid: this.fCompanyid
            });
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[2].disabled = false;
            this.formObj[4].disabled = false;
            this.formObj[6].uploadSelect = true;
        } else if (this.statusNum == 2) { //编辑
            //编辑
            this.formData = this.$options.data.call(this).formData;
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[2].disabled = false;
            this.formObj[4].disabled = false;
            this.formObj[6].uploadSelect = true;
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[6].uploadSelect = false;
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // fCcpeopleData计划传阅人
        selectChange(data, a, b) {
            if (b == "fWeeklyreportdates") {
                this.formData.fWeeklyreportdates = data
                this.getWeeks(data)
            }
        },
        getWeeks(data) {
            getWeeks({
                fCompanyid: this.userInfo.companyId,
                startDates: data
            }).then(res => {
                if (res.errcode == 'ok') {
                    this.formData.fReportingweek = res.msg
                }
            })
        },
        edit(status) {
            if (status == 1) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 2) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 4) {
                this.monthExamineData = this.$options.data.call(this).monthExamineData;
            }
        },
        // 详情
        getDetail(data1) {
            getJobbriefingdataByfId(data1).then((res) => {
                var data = res.data.yjQmpMsJobbriefingdata;
                if (data1.fId != '') {
                    this.formData = data;
                    this.formDatafileList = res.data.fAnnexlist;
                    this.formData.recordfiles = []
                    let curDate = (new Date()).getTime();
                    let three = this.getDaysBetween(new Date(res.data.startDates), new Date(this.common.getDay(0))) * 24 * 3600 * 1000;
                    this.startDates = curDate - three;
                } else {
                    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
                    this.formData.fWeeklyreportdates = this.common.getDay(0)
                    this.formData.fReportdates = this.common.getDay(0);
                    this.formData.fProjectname = this.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
                    this.formData.fProjectid = this.projectId; //当前登录人项目ID
                    this.formData.fCompanyname = this.userInfo.companyName; //当前登录人标段名称
                    this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID
                    this.formData.fReportid = this.userInfo.userId; //填报人id
                    this.formData.fReportname = this.userInfo.realName; //填报人名称
                    this.formData.fAnnex = data.fAnnex //资料附件ID
                    this.formData.fReportingweek = data.fReportingweek
                    // this.formData.fSubunitid = this.fCompanyid;
                    this.formData.fSubunitid = this.userInfo.companyId;
                    this.formData.fSubunit = this.userInfo.companyName;
                    this.formData.recordfiles = []
                    let curDate = (new Date()).getTime();
                    let three = this.getDaysBetween(new Date(res.data.startDates), new Date(this.common.getDay(0))) * 24 * 3600 * 1000;
                    this.startDates = curDate - three;
                }
            });
        },
        getDaysBetween(date1, date2) {
            const ONE_DAY = 1000 * 60 * 60 * 24; // 一天的毫秒数
            const date1Time = date1.getTime(); // 获取时间戳
            const date2Time = date2.getTime();
            const difference = Math.abs(date1Time - date2Time); // 获取时间差
            return Math.round(difference / ONE_DAY); // 两个日期之间的天数
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.formDatafileList.length; index++) {
                if (file.uid == this.formDatafileList[index].uid) {
                    if (this.formDatafileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.formDatafileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.formDatafileList[index].atta.fId,
                        });
                    }
                    this.formDatafileList.splice(index, 1);
                }
            }
        },
        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            if (this.statusNum == 1) { //新增
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 2) { //编辑
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            }
        },
        // 保存
        saveForm(data) {
            this.isLock = true
            saveJobbriefingdata(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        if (res.msg == 'error') {
                            this.$message.error('该周次已上报，禁止重复上报');
                        } else {
                            this.visible = false;
                            this.$emit("refreshList");
                            this.$message.success("新增成功");
                        }
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};